package com.godyao.service.impl;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.godyao.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author godyao
 * @date 2022/3/16
 */
@Primary
@Component
//@CacheConfig(cacheNames = "secKill:user")
public class JwtTokenUserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private LoginService loginService;

    /**
     * @Cacheable redis缓存 平均qps 842
     * @Cached 二级缓存 平均qps 1261
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    //@Cacheable
    @Cached(name = "UserDetailsService.loadUserByUsername", expire = 3600, cacheType = CacheType.BOTH)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return loginService.loadByUsername(username);
    }
}
